package sz.szsmk.citizencard.ui.newversion.fragment.presenter

import android.util.Log
import com.youzan.spiderman.utils.MD5Utils
import sz.szsmk.citizencard.DaggerApplication
import sz.szsmk.citizencard.model.network.bean.request.*
import sz.szsmk.citizencard.model.network.bean.response.*
import sz.szsmk.citizencard.mvp.dzsmkpresenter.AppBasePresenter
import sz.szsmk.citizencard.retrofitrxjava.newversion.ApiCallback
import sz.szsmk.citizencard.retrofitrxjava.newversion.HttpDateSource
import sz.szsmk.citizencard.smkretrofit.NetworkDateSource
import sz.szsmk.citizencard.ui.adapter.bean.AppHomeCardBean
import sz.szsmk.citizencard.ui.newversion.fragment.view.MeMvpView
import sz.szsmk.citizencard.util.ACache
import sz.szsmk.citizencard.util.AndroidUtils
import sz.szsmk.citizencard.util.Constant
import sz.szsmk.citizencard.util.StringUtils
import javax.inject.Inject

/**
 *  author : hyt
 *  date : 2020/10/20
 *  description :
 */
class MePresenter @Inject constructor(var httpDateSourceNotUse: NetworkDateSource)
    : AppBasePresenter<MeMvpView>(){

    //获取用户信息
    fun getUserInfo(){
        val userInfoReq = IBaseRequest()
        val access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token")
        userInfoReq.access_token = access_token
        val androidId = AndroidUtils.getAndroidId(DaggerApplication.getIns())
        userInfoReq.device_id = AndroidUtils.getAndroidId(DaggerApplication.getIns())
        val refresh_token = ACache.get(DaggerApplication.getIns()).getAsString("refresh_token")
        val key = "access_token=$access_token&device_id=$androidId&key=$refresh_token"
        val stringMd5 = MD5Utils.getStringMd5(key).toUpperCase()
        userInfoReq.sign = stringMd5

        addSubscription(HttpDateSource.createApi()?.getUserInfoZhsz(userInfoReq), object : ApiCallback<IUserInfo?> {
            override fun onSuccess(response: IUserInfo?) {
                response?.let {
                    mvpView.getUserInfoSuccess(it)
                }
            }

            override fun onFailure(model: BaseBean?, e: Throwable?) {
                model?.let { mvpView.onFail(it) }
            }
        })
    }

    //获取用户等级
    fun getUserLevel(){
        val baseRequest = IBaseRequest()
        val access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token")
        baseRequest.access_token = access_token
        val androidId = AndroidUtils.getAndroidId(DaggerApplication.getIns())
        baseRequest.device_id = AndroidUtils.getAndroidId(DaggerApplication.getIns())
        val refresh_token = ACache.get(DaggerApplication.getIns()).getAsString("refresh_token")
        val key = "access_token=$access_token&device_id=$androidId&key=$refresh_token"
        val stringMd5 = MD5Utils.getStringMd5(key).toUpperCase()
        baseRequest.sign = stringMd5

        addSubscription(HttpDateSource.createApi()?.firstSearchUserLevel(baseRequest), object : ApiCallback<FirstSearchUserLevelRes?> {
            override fun onSuccess(response: FirstSearchUserLevelRes?) {
                response?.let {
                    val appHomeCardBean = AppHomeCardBean()
                    if (!StringUtils.isBlank(response.data.recognitionLevel.l3)) {
                        appHomeCardBean.level = "L3"
                        appHomeCardBean.levelDesc = response.data.recognitionLevel.l3
                    } else if (!StringUtils.isBlank(response.data.recognitionLevel.l2)) {
                        appHomeCardBean.level = "L2"
                        appHomeCardBean.levelDesc = response.data.recognitionLevel.l2
                    } else if (!StringUtils.isBlank(response.data.recognitionLevel.l1)) {
                        appHomeCardBean.level = "L1"
                        appHomeCardBean.levelDesc = response.data.recognitionLevel.l1
                    }
                    if (response.data.recognitionLevel != null) {
                        val l2 = response.data.recognitionLevel.l2
                        if (l2 != null) {
                            val split = l2.split("\\+".toRegex()).toTypedArray()
                            val idCard = split[1]
                            appHomeCardBean.idCard = idCard
                            Constant.USER_IDCARD = idCard
                        }
                    }
                    Constant.USER_NAME = response.data.custName
                    appHomeCardBean.custName = response.data.custName
                    appHomeCardBean.ecityCardNo = response.data.ecityCardNo
                    mvpView.getUserLevelSuccess(appHomeCardBean)
                }
            }

            override fun onFailure(model: BaseBean?, e: Throwable?) {
                model?.let { mvpView.onFail(it) }
            }
        })
    }

    //获取积分
    fun getUserCredit(){
        val signInReq = SignInReq()
        signInReq.access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token")

        addSubscription(HttpDateSource.createApi()?.getScore(signInReq), object : ApiCallback<SignInRes?> {
            override fun onSuccess(response: SignInRes?) {
                response?.let {
                    mvpView.getUserCreditSuccess(it)
                }
            }

            override fun onFailure(model: BaseBean?, e: Throwable?) {
                model?.let { mvpView.onFail(it) }
            }
        })
    }

    //获取优惠券
    fun getCouponCount(){
        val accessTokenRequest = AccessTokenRequest()
        accessTokenRequest.access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token")
        addSubscription(HttpDateSource.createApi()?.queryCouponCount(accessTokenRequest), object : ApiCallback<CouponListBean?> {
            override fun onSuccess(response: CouponListBean?) {
                response?.let {
                    mvpView.getCouponCountSuccess(it)
                }
            }

            override fun onFailure(model: BaseBean?, e: Throwable?) {
                model?.let { mvpView.onFail(it) }
            }
        })
    }

    //获取认证信息   社保  公积金  桂花分
    fun getUserAuthInfo(){
        val accessTokenRequest = AccessTokenRequest()
        accessTokenRequest.access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token")
        addSubscription(HttpDateSource.createApi()?.getUserAuthInfo(accessTokenRequest), object : ApiCallback<SeleciUserAuthRes?> {
            override fun onSuccess(response: SeleciUserAuthRes?) {
                response?.let {
                    mvpView.getUserAuthInfoSuccess(it)
                }
            }

            override fun onFailure(model: BaseBean?, e: Throwable?) {
                model?.let { mvpView.onFail(it) }
            }
        })
    }

    //获取NG 金额
    fun getNgFinance() {
        val queryNgFinanceReq = QueryNgFinanceReq()
        queryNgFinanceReq.flag = "1"
        queryNgFinanceReq.access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token")
        addSubscription(HttpDateSource.createApi()?.queryNgFinance(queryNgFinanceReq), object : ApiCallback<QueryNgFinanceRes?> {
            override fun onSuccess(response: QueryNgFinanceRes?) {
                response?.let {
                    mvpView.getNgFinanceSuccess(it)
                }
            }

            override fun onFailure(model: BaseBean?, e: Throwable?) {}
        })
    }

    //获取订单数据
    fun getOrderCount(){
        val accessTokenRequest = AccessTokenRequest()
        addSubscription(HttpDateSource.createApi()?.queryOrderCount(accessTokenRequest), object : ApiCallback<QueryOrderCountRes?> {
            override fun onSuccess(response: QueryOrderCountRes?) {
                response?.let {
                    mvpView.getOrderCountSuccess(it)
                }
            }

            override fun onFailure(model: BaseBean?, e: Throwable?) {}
        })
    }

    //获取是否显示钱包 资产
    fun getCommonConfig(){
        val baseRequest = IBaseRequest()
        val access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token")
        baseRequest.access_token = access_token
        val androidId = AndroidUtils.getAndroidId(DaggerApplication.getIns())
        baseRequest.device_id = AndroidUtils.getAndroidId(DaggerApplication.getIns())
        val refresh_token = ACache.get(DaggerApplication.getIns()).getAsString("refresh_token")
        val key = "access_token=$access_token&device_id=$androidId&key=$refresh_token"
        val stringMd5 = MD5Utils.getStringMd5(key).toUpperCase()
        baseRequest.sign = stringMd5

        addSubscription(HttpDateSource.createApi()?.queryCommonConfig(baseRequest), object : ApiCallback<MeConfigInfoListRes?> {
            override fun onSuccess(response: MeConfigInfoListRes?) {
                response?.let {
                    mvpView.getCommonConfigSuccess(it)
                }
            }

            override fun onFailure(model: BaseBean?, e: Throwable?) {}
        })
    }


    //校验桂花分账号是否存在
    fun accountExist(level: String?, idCard: String?, phoneNum: String?, realName: String?){
        val accountExist= AccountExistReq()
        if (level.equals("1.0")){
            accountExist.phoneNum=phoneNum
            addSubscription(HttpDateSource.createApigfh()?.queryAccountExist(accountExist), object : ApiCallback<AccountExistRes?> {
                override fun onSuccess(response: AccountExistRes?) {
                    response?.let {
                        Log.e("queryAccountExist", "进入LV3")
                        Log.e("queryAccountExist",response.msg)

                        mvpView.accountExist(it)
                    }
                }

                override fun onFailure(model: BaseBean?, e: Throwable?) {}
            })
        }else{
            accountExist.phoneNum=phoneNum
            addSubscription(HttpDateSource.createApigfh()?.queryAccountExist(accountExist), object : ApiCallback<AccountExistRes?> {
                override fun onSuccess(response: AccountExistRes?) {
                    response?.let {
                        Log.e("queryAccountExist",response.msg)
                        Log.e("queryAccountExist", "进入LV1 2")
                        mvpView.accountExist(it)
                    }
                }

                override fun onFailure(model: BaseBean?, e: Throwable?) {
                    Log.e("queryAccountExist", e?.message)
                }
            })
        }
    }

}